<?php
declare(strict_types=1);

namespace App\Common\Event;

use Hyperf\Crontab\Event\AfterExecute;
use Hyperf\Crontab\Event\BeforeExecute;
use Hyperf\Crontab\Event\FailToExecute;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use App\Common\Modular\System\Serve\CrontabServe;

#[Listener]
class CrontabListener implements ListenerInterface
{

    #[Inject]
    protected CrontabServe $serve;


    public function listen(): array
    {
        return [
            # CrontabDispatcherStarted::class,//任务管理器启动事件
            AfterExecute::class,//任务执行前触发
            BeforeExecute::class,//任务执行后触发
            FailToExecute::class//任务执行失败触发
        ];
    }

    /**
     * @param object $event
     */
    public function process(object $event): void
    {
        if ($event instanceof BeforeExecute) {
            $this->serve->setCrontabLog($event->crontab->getName(), true);
        } elseif ($event instanceof FailToExecute) {
            $this->serve->setCrontabLog($event->crontab->getName(), false);
        }
    }
}